/*
 * @Author: wwssaabb
 * @Date: 2021-07-27 08:26:08
 * @LastEditTime: 2021-08-13 17:25:10
 * @FilePath: \react-admin-client\src\api\ajax.js
 */

/* 
  能发送异步ajax请求函数模块，返回promise对象
  封装axios库
  1.优化：统一处理请求去异常
*/

import axios from 'axios'
import { message } from 'antd'

const BASEURL = 'http://8.129.105.10:3333/api'

export default function ajax(url,data,method='GET',callback){
  return new Promise((resolve,reject)=>{
    let promise

    if(method==='GET'){
      promise = axios.get(BASEURL+url,{params:data})
    }else{
      promise = axios.post(BASEURL+url,data)
    }

    promise.then(res=>resolve(res.data))
      .catch(error=>message.error(error.message))
    
    if(callback)callback()
  })
}